package com.wd.gui;

import com.wd.dominio.Lugar;
import com.wd.gui.controlparticular.ControlGuiCentroDistribucion;
import com.wd.gui.controlparticular.ControlGuiLugar;
import java.util.Vector;

/**
 *
 * @author Gabylis
 */
public class VentanaAgregarCentro extends javax.swing.JFrame {

    private ControlGuiCentroDistribucion controlCentro;

    private ControlGuiLugar controlLugar;

    private ControlGui controlGeneral;

    private Vector<Lugar> estados;

    private Vector<Lugar> ciudades;

    /** Creates new form VentanaAgregarCentro */
    public VentanaAgregarCentro(Vector<Lugar> result) {
        initComponents();
        java.net.URL url = getClass().getResource("Iconos/icon_016.png");
        java.awt.Image imagen = getToolkit().getImage(url);
        setIconImage (imagen);

        ciudades = new Vector();
        estados = new Vector();
        estados = result;

        for (Lugar lugar : result){
            this.comboEstado.addItem(lugar.getNombrePropio());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();
        jPanelAgregarCentro = new javax.swing.JPanel();
        labelEstado = new java.awt.Label();
        labelCiudad = new java.awt.Label();
        labelNombre = new java.awt.Label();
        labelTelefono = new java.awt.Label();
        labelDireccion = new java.awt.Label();
        comboEstado = new javax.swing.JComboBox();
        comboEstado.setSelectedIndex(-1);
        comboCiudad = new javax.swing.JComboBox();
        comboCiudad.setSelectedIndex(-1);
        campoNombre = new javax.swing.JTextField();
        campoTelefono = new javax.swing.JTextField();
        campoDireccion = new javax.swing.JTextField();
        jButtonAceptar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        setTitle("Agregar Centro Distribución");

        jPanelAgregarCentro.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Nuevo Centro de Distribución", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12), new java.awt.Color(0, 102, 204))); // NOI18N
        jPanelAgregarCentro.setToolTipText("Agregar Centro de Distribución");

        labelEstado.setFont(new java.awt.Font("Verdana", 1, 12));
        labelEstado.setText("Estado");

        labelCiudad.setFont(new java.awt.Font("Verdana", 1, 12));
        labelCiudad.setText("Ciudad");

        labelNombre.setFont(new java.awt.Font("Verdana", 1, 12));
        labelNombre.setText("Nombre Centro");

        labelTelefono.setFont(new java.awt.Font("Verdana", 1, 12));
        labelTelefono.setText("Teléfono");

        labelDireccion.setFont(new java.awt.Font("Verdana", 1, 12));
        labelDireccion.setText("Dirección");

        comboEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboEstadoActionPerformed(evt);
            }
        });

        jButtonAceptar.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jButtonCancelar.setFont(new java.awt.Font("Tahoma", 1, 11));
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelAgregarCentroLayout = new javax.swing.GroupLayout(jPanelAgregarCentro);
        jPanelAgregarCentro.setLayout(jPanelAgregarCentroLayout);
        jPanelAgregarCentroLayout.setHorizontalGroup(
            jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAgregarCentroLayout.createSequentialGroup()
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelAgregarCentroLayout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(labelEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(labelTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(comboEstado, 0, 250, Short.MAX_VALUE)
                    .addComponent(comboCiudad, 0, 250, Short.MAX_VALUE)
                    .addComponent(campoNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .addComponent(campoTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .addComponent(campoDireccion, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelAgregarCentroLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jButtonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addComponent(jButtonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
        );
        jPanelAgregarCentroLayout.setVerticalGroup(
            jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelAgregarCentroLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(campoDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanelAgregarCentroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jPanelAgregarCentro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .addComponent(jPanelAgregarCentro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed

       int resultado = -1;
       int lugarId = 0;
       String nombre = "";
       String telefono = "";
       String direccion = "";
       String nombreCiudad = "";

       this.controlGeneral =  new ControlGui ();
       resultado = this.controlGeneral.dialogoConfirmacion("¿Está seguro " +
               "que desea realizar esta operación?");
       if (resultado == 0) {
           nombreCiudad = this.comboCiudad.getSelectedItem().toString();
           lugarId = this.buscarCiudadId(nombreCiudad);
           nombre = this.campoNombre.getText().toUpperCase();
           telefono   = this.campoTelefono.getText().toUpperCase();
           direccion  = this.campoDireccion.getText().toUpperCase();
           this.controlCentro = new ControlGuiCentroDistribucion ();
           controlCentro.agregarCentroDistribucion(0, nombre, telefono,
           direccion, lugarId);
       }
       this.reiniciarFields();
    }//GEN-LAST:event_jButtonAceptarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void comboEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboEstadoActionPerformed
        int select = -1;
        select = this.comboEstado.getSelectedIndex();
        //if (this.comboCiudad.getItemCount()>0){
            Lugar estadoDueño = new Lugar();
            estadoDueño = estados.elementAt(select);
            cargarCiudades(estadoDueño);
        //}
    }//GEN-LAST:event_comboEstadoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<Lugar> result) {
                new VentanaAgregarCentro(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField campoDireccion;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoTelefono;
    private javax.swing.JComboBox comboCiudad;
    private javax.swing.JComboBox comboEstado;
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JPanel jPanelAgregarCentro;
    private java.awt.Label labelCiudad;
    private java.awt.Label labelDireccion;
    private java.awt.Label labelEstado;
    private java.awt.Label labelNombre;
    private java.awt.Label labelTelefono;
    // End of variables declaration//GEN-END:variables

    public void cargarCiudades(Lugar Estado){
        this.comboCiudad.removeAllItems();

        int codigoEstado = -1;
        codigoEstado = Estado.getId();

        controlLugar = new ControlGuiLugar();
        ciudades = controlLugar.traerTodosLosLugares(2);

        for (Lugar lugar : ciudades) {
            if (lugar.getLugarID() == codigoEstado){
                this.comboCiudad.addItem(lugar.getNombrePropio());
            }
        }
    }

    public int buscarCiudadId(String nombre){
        int id = -1;
        for (Lugar lugar : ciudades){
            if (lugar.getNombrePropio().equals(nombre)){
                id = lugar.getId();
                break;
            }
        }
        return id;
    }

    public void reiniciarFields(){
        this.campoNombre.setText("");
        this.campoTelefono.setText("");
        this.campoDireccion.setText("");
    }
}
